using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Core;

namespace Tester.GuiActions.Actions.Impl
{
    public delegate void UIChanged();

    public abstract class BaseAction : IAction
    {
        public class FindMainMenuItemVisitor : IMainMenuVisitor
        {
            private IAction _action;
            private string _menuName = null;
            
            public FindMainMenuItemVisitor(IAction action)
            {
                _action = action;
            }

            public string MenuName
            {
                get
                {
                    return _menuName;
                }
            }

            public void Visit(MainMenuManager.MainMenuItem value)
            {
                if (value.Action != null && value is MainMenuManager.MainMenuMenuItem)
                {
                    if (value.Action.Name == _action.Name)
                    {
                        _menuName = (value as MainMenuManager.MainMenuMenuItem).Display;
                    }
                }
                value.Accept(this);
            }
        }

        private string _toolBarName = null;

        public string ToolBarName
        {
            get
            {
                return _toolBarName;
            }
        }

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value; 
            }
        }

        public virtual bool Visible
        {
            get
            {
                return true;
            }
        }

        private string _description;
        public string Description
        {
            get
            {
                return _description ?? "";
            }
            set
            {
                _description = value; 
            }
        }

        public event UIChanged UIChanged;

        public virtual void CustomActionForMenu(MainMenuManager.MainMenuItem item)
        {
        }

        private List<Context> _contexts;

        public void AddContext(string name, string display)
        {
            _contexts = _contexts?? new List<Context>();
            _contexts.Add(new Context(name, display));            
        }

        public abstract void MainAction(object sender, EventArgs e);

        public void AddUIEvent(string name)
        {
            typeof(UIEvents).GetEvent(name).AddEventHandler(UIEvents.Instance, new UIChangedDelegate(FireUIChange));
        }

        public void AddToolBar(string name)
        {
            _toolBarName = name;
        }

        private void FireUIChange()
        {
            if (UIChanged != null)
            {
                UIChanged(); 
            }
        }

        public override string ToString()
        {
            FindMainMenuItemVisitor visitor = new FindMainMenuItemVisitor(this);
            Actions.Instance.MainMenuManager.RootItem.Accept(visitor);
            if (visitor.MenuName != null)
            {
                return visitor.MenuName;
            } else
            {
                return Name ?? "";
            }
        }
    }
}
